#! /bin/bash

# --------------------------------------------------
# Project monitor:
#       https://github.com/Statemood/monitor
#
#
# --------------------------------------------------

   tag=1
 count=1
number=1
  temp=`mktemp /tmp/XXXXXXXXXXXX`
config="$(dirname $(dirname $0))/etc/http-discovery.list"

# Generate http discovery list ($config)
$(dirname $0)/generate_http_discovery_list

if [ ! -f "$config" ]
then
    echo "Error: config file not found"
    exit 1
fi

PrintDiscoveryList(){
    for i in $list
    do
        n=1
        if [ $tag = 0 ]
        then
            host="$ip_head.$i"
        else
            test "$host" = "0" && host="$ip_head.$i" && n=0
        fi

         server="$ip_range0-$i"
        items_0="{\"{#HTTP.SERVICE}\": \"$service\", \"{#HTTP.SERVER}\": \"$server\", "
        items_1="\"{#HTTP.HOST}\": \"$host\", \"{#HTTP.IP}\": \"$ip_head.$i\", "
        items_2="\"{#HTTP.PORT}\": $port, \"{#HTTP.CODE}\": $code, \"{#HTTP.URI}\": \"$uri\", "
        items_3="\"{#HTTP.ID}\": \"$id\"}"

        echo "$items_0$items_1$items_2$items_3"    >> $temp

        test $n = 0 && host=0
    done
}

grep -v "^#" $config | sed '/^$/d' |\
while read line
do
    service="`echo $line | awk -F ':' '{print $1}'`"
       host="`echo $line | awk -F ':' '{print $2}'`"
         ip="`echo $line | awk -F ':' '{print $3}'`"
       port="`echo $line | awk -F ':' '{print $4}'`"
       code="`echo $line | awk -F ':' '{print $5}'`"
        uri="`echo $line | awk -F ':' '{print $6}'`"
         id="`echo $line | awk -F ':' '{print $7}'`"

    test -z "$host" && host=0
    test -z "$ip"   &&   ip=0   && tag=0
    test -z "$port" && port=80
    test -z "$code" && code=200
    test -z "$uri"  &&  uri="/"

    test "$ip" = "$host" && test "$ip" = 0 && continue

    ip_head="`echo $ip | awk -F '.' '{print $1,$2,$3}' | sed 's/ /./g'`"
    ip_tail="`echo $ip | awk -F '.' '{print $4}'`"

    # for 192.168.1.10-20, 22-25
    test $ip = 0 && ip="$host"
    echo "$ip" | grep "\-" | grep -q ","
    if [ $? = 0 ]
    then
        for ipa in `echo $ip_tail | sed 's/,/ /g'`
        do
            for s in $ipa
            do
                ip_range1="`echo $s | awk -F '-' '{print $1}'`"
                ip_range2="`echo $s | awk -F '-' '{print $2}'`"

                test -z "$ip_range2" && ip_range2=$ip_range1

                for ((list=$ip_range1; list<=$ip_range2; list++))
                do
                    PrintDiscoveryList
                done
            done
        done

        continue
    fi

    # for 192.168.1.10-20
    echo "$ip" | grep -q "\-"
    if [ $? = 0 ]
    then
        # transfer 10-20 to 10 and 20
        ip_range0="`echo $ip | awk -F '.' '{print $3}'`"
        ip_range1="`echo $ip | awk -F '.' '{print $4}' | awk -F '-' '{print $1}'`"
        ip_range2="`echo $ip | awk -F '.' '{print $4}' | awk -F '-' '{print $2}'`"

        test -z "$ip_range2" && ip_range2=$ip_range1

        for ((list=$ip_range1; list<=$ip_range2; list++))
        do
            PrintDiscoveryList
        done
        continue
    fi

    # for 192.168.1.10,20,30
    echo "$ip" | grep -q ','
    if [ $? = 0 ]
    then
        for list in `echo $ip | awk -F '.' '{print $4}' | sed 's/,/ /g'`
        do
            PrintDiscoveryList
        done
    fi
done

total=`wc -l $temp | awk '{print $1}'`
printf  '{\n\t'"\"data\":["
while read line
do
    if [ $count = $total ]
    then
        echo "$line"
        #echo "{\"{#TOTAL.ITEMS}\": $total}"
    else
        echo "$line,"
        echo $((count++)) > /dev/null
    fi
done < $temp

printf  "\n\t]\n}\n"

test -f "$temp" && rm -rf "$temp"
